
package com.joyo8.app;

import android.app.Service;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;

public class DXService extends Service {
    private MyBinder mBinder = new MyBinder();
    private SmsThread mSmsThread = null;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return mBinder;
    }
    
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            Constants.mUtil.logd("handler msg what: "+msg.what);
            switch (msg.what){
                case Constants.MSG_QUERY_NEW_MSG:
                    queryNewSms();
                    break;
            }
        }
    };

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Constants.mUtil.logd("dxservice create");
        if(mSmsThread == null){
            mSmsThread = new SmsThread();
            mSmsThread.start();
        }
        this.getContentResolver().registerContentObserver(// 监听短信数据库改变
                // Telephony.Sms.CONTENT_URI,
                Uri.parse("content://sms"), true, new SmsContentObserver(mHandler));
    }
    
    
    class SmsThread extends Thread{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Looper.prepare();
            Constants.mUtil.logd("SmsThread run ~");
            if(mHandler == null){
                mHandler = new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        // TODO Auto-generated method stub
                        super.handleMessage(msg);
                        Constants.mUtil.logd("handler msg what: "+msg.what);
                        switch (msg.what){
                            case Constants.MSG_QUERY_NEW_MSG:
                                queryNewSms();
                                break;
                        }
                    }
                };
            }
            
            Looper.loop();
        }
        
    }
    
    private void queryNewSms(){
/*        Uri uri = Uri.parse("content://sms/");
        Cursor cursor = getContentResolver().query(uri,
                new String[] {
                        "_id", "address", "read", "body"
                }, " read=?", new String[] {
                    "0"
                }, "date desc");
        if (cursor != null && cursor.moveToFirst()) {
            while (cursor.isLast()) {
                String body = cursor.getString(3);
                Constants.mUtil.logd("message: "+body);
                cursor.moveToNext();
            }
            cursor.close();
        }else{
            Constants.mUtil.logd("sms cursor is null");
        }*/
        
        Uri uri = Uri.parse("content://sms/inbox/");
        Cursor cur = getContentResolver().query(uri,
                new String[] {
                "_id", "address", "read", "body"
        }, " read=?", new String[] {
            "0"
        }, "date desc");
        if (cur != null && cur.moveToFirst()) {
            do {
                for (int j = 0; j < cur.getColumnCount(); j++) {
                    String info = "name:" + cur.getColumnName(j) + "=" + cur.getString(j);
                    Constants.mUtil.logd(" => "+info);
                }
            } while (cur.moveToNext());
        }else{
            Constants.mUtil.logd(" fk! sms cursor is null");
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Constants.mUtil.logd("dxservice start");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        return super.onUnbind(intent);
    }

    class MyBinder extends Binder {
        public DXService getService() {
            return DXService.this;
        }
    }

    

}
